//
//  MCBaseLotteryHallViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/7.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCBaseLotteryHallViewController.h"
#import "MCPullMenuViewController.h"
#import "MCPullMenuPresentationController.h"
#import "MCPickBottomButton.h"
#import "UIImage+Extension.h"
#import "MCMoneyPopView.h"
#import "MCMSPopView.h"
#import "NSString+Helper.h"
#import "MCWFExplainViewController.h"
#import "MCMoreNavView.h"
#import "MCGameRecordViewController.h"
#import "MCGetplayawardModel.h"
#import "MCMaxbonusModel.h"
#import "MCIssueModel.h"
#import "MCLotteryID.h"
#import "MCMaxbonusModel.h"
#import "MCUserMoneyModel.h"
#import "MCGetMerchantInfoModel.h"
#import "MCDataTool.h"
#import "MCMineInfoModel.h"
#import "MCHistoryIssueDetailAPIModel.h"
#import "MCChaseNumberViewController.h"
#import "MCPickNumberViewController.h"

@interface MCBaseLotteryHallViewController ()
<MCPullMenuDelegate>

#pragma mark - property
/**定时器*/
@property(nonatomic,strong) NSTimer *timer;
// 时间
@property (nonatomic,weak) UILabel *timerDetailLabel;
// 期数
@property (nonatomic,weak) UILabel *timerLabel;
/**弹出视图*/
@property (nonatomic,weak) MCAlertView *alertView;
/**底部视图*/
@property (nonatomic,weak) UIView *bottomView;

/**下拉按钮 玩法名称*/
@property (nonatomic,weak) UIButton *btnPCatergy;
/**近期开奖*/
@property (nonatomic,weak) UIButton *btnKaiJiang;

/**倒计时按钮*/
@property (nonatomic,weak) UIButton *btnDaojishi;

/**期号string*/
@property (nonatomic,strong) NSString *lastString;
/**随机按钮状体*/
@property (nonatomic,assign) BOOL randomBtnSelected;
/**随机按钮*/
@property (nonatomic,weak) UIButton *randomBtn;

/**投注页右上角功能按钮*/
@property (nonatomic,weak)MCMoreNavView * moreNavView;
/**isShowMoreBtn 是否展示按钮*/
@property (nonatomic,assign)BOOL isShowMoreBtn;
/**playawardModel模型的强引用*/
@property (nonatomic,strong)MCGetplayawardModel *playawardModel;

@property (nonatomic,strong)MCMaxbonusModel *bonusModel;

@property (nonatomic,strong)MCIssueModel *issueModel;

@property (nonatomic,strong)MCGetMerchantInfoModel * getMerchantInfoModel;
//每次进入页面   余额都要刷新
@property (nonatomic,weak)UILabel *bonusDetailLabel;
//期号加载菊花
@property (nonatomic, strong) UIActivityIndicatorView * activityIndicator;
//追号完成后  余额进行刷新
@property (nonatomic, strong) MCUserMoneyModel * userMoneyModel;
@end

@implementation MCBaseLotteryHallViewController


- (NSMutableArray *)boModelArray{
    if (_boModelArray == nil) {
        _boModelArray = [NSMutableArray array];
    }
    return _boModelArray;
}
-(UIActivityIndicatorView *)activityIndicator{
    
    if (!_activityIndicator) {
        _activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
        //设置小菊花的frame
        _activityIndicator.frame= CGRectMake(G_SCREENWIDTH/4.0, 0, HEIGHTDAOJISHI, HEIGHTDAOJISHI);
        //设置小菊花颜色
        _activityIndicator.color = RGB(100, 100, 100);
        //设置背景颜色
        _activityIndicator.backgroundColor = [UIColor clearColor];
        //刚进入这个界面会显示控件，并且停止旋转也会显示，只是没有在转动而已，没有设置或者设置为YES的时候，刚进入页面不会显示
        [_activityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏
    }
    return _activityIndicator;
}
#pragma mark - life cycle
- (void)viewDidLoad {

    [super viewDidLoad];

    [self setUpUI];
    //为导航栏添加右侧按钮(更多)
    [self addNavRightBtn];
    //底部按钮
    [self setUpBottomView];
    MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    userMoneyModel.isNeedLoad=NO;
    
    [self selectLotteryKindWithTag:[MCPullMenuViewController Get_DefaultDicWithID:self.lotteriesTypeModel.LotteryID and:self.palyCode]];
    
    [self loadData];

    [self jungeIsMMCType];
}

- (void)jungeIsMMCType{
     MCMaxbonusModel *model =  [MCMaxbonusModel shareInstance];
    if ([self.baseWFmodel.LotteryID isEqualToString:@"50"]) {
       //极限秒秒彩
        model.mmcType = MMCBET;
    } else {
        model.mmcType = OTHERBET;
    }

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self stopTimer];
    [self.alertView hideStackWindow];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    __weak typeof(self) weakSelf = self;
    MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    if (userMoneyModel.isNeedLoad||userMoneyModel.LotteryMoney.length<1) {
        [userMoneyModel refreashDataAndShow];
        self.userMoneyModel=userMoneyModel;
        userMoneyModel.callBackSuccessBlock = ^(id manager) {
            weakSelf.userMoneyModel.FreezeMoney=manager[@"FreezeMoney"];
            weakSelf.userMoneyModel.LotteryMoney=manager[@"LotteryMoney"];
            weakSelf.bonusDetailLabel.text = [NSString stringWithFormat:@"%@元",[MCMathUnits GetMoneyShowNum:manager[@"LotteryMoney"]]];
        };
    }else{
        _bonusDetailLabel.text = [NSString stringWithFormat:@"%@元",[MCMathUnits GetMoneyShowNum:userMoneyModel.LotteryMoney]];
    }
    [self registerNotifacation];
    [self loadIssueData];
    if (![[NSUserDefaults standardUserDefaults] objectForKey:MerchantMaxRebate]) {
        
        MCGetMerchantInfoModel *model = [MCGetMerchantInfoModel sharedMCGetMerchantInfoModel];
        [model refreashDataAndShow];
        self.getMerchantInfoModel = model;
        model.callBackSuccessBlock = ^(id manager) {
            
            [self setBetListMarry];
            
        };
    }else{
        [self setBetListMarry];
    }
    
    if ([self isKindOfClass:[MCChaseNumberViewController class]]) {
        MCUserDefinedLotteryCategoriesModel *model = [MCUserDefinedLotteryCategoriesModel GetMCUserDefinedLotteryCategoriesModelWithCZID:self.baseWFmodel.LotteryID];
        MCPickNumberViewController *pickVc = [[MCPickNumberViewController alloc] init];
        pickVc.lotteriesTypeModel = model;
        pickVc.navigationItem.title = [MCLotteryID getLotteryCategoriesNameByID:self.baseWFmodel.LotteryID];
        [self.navigationController pushViewController:pickVc animated:YES];
    }
    
    
}

#pragma mark-设置列表返点数据
-(void)setBetListMarry{
    
    self.baseWFmodel.shangHuRebate = [[NSUserDefaults standardUserDefaults] objectForKey:MerchantMaxRebate];
    self.baseWFmodel.shangHuMinRebate = [[NSUserDefaults standardUserDefaults] objectForKey:MerchantMinRebate];
    self.baseWFmodel.XRebate = [[NSUserDefaults standardUserDefaults] objectForKey:MerchantXRebate];
    self.baseWFmodel.Mode=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:MerchantMode]];
    
    NSMutableArray * marr=[[NSMutableArray alloc]init];
    int mode= [self.baseWFmodel.Mode intValue];
    [marr addObject:@"元模式"];
    if ((mode&2) ==2 ) {
        [marr addObject:@"角模式"];
    }
    if ((mode&1) ==1 ) {
        [marr addObject:@"分模式"];
    }
    if ((mode&8) ==8 ) {
        [marr addObject:@"厘模式"];
    }

    MCMSPopView * pView=[MCMSPopView alertInstance];
    pView.dataArray=marr;
    
    MCMineInfoModel * minInfoModel=[MCMineInfoModel sharedMCMineInfoModel];
    self.baseWFmodel.userRegisterRebate=minInfoModel.MyRebate;
    
    
    if (self.baseWFmodel.XRebate&&self.baseWFmodel.shangHuRebate&&self.baseWFmodel.userRegisterRebate&&self.baseWFmodel.czRebate&&self.baseWFmodel.czTZRebate&&self.baseWFmodel.shangHuMinRebate) {
        
        _betRebateArray=[MCDataTool GetShowBetRebateMarryWithModel:self.baseWFmodel];
        
        MCMoneyPopView * popView=[MCMoneyPopView alertInstance];
        
        popView.dataArray=_betRebateArray;
        
        MCShowBetModel * betModel=_betRebateArray[0];
        
        self.baseWFmodel.userSelectedRebate=betModel.BetRebate;
        self.baseWFmodel.showRebate=betModel.showRebate;
        
        
        [self.alertView.moneyBtn setTitle:betModel.showRebate forState:UIControlStateNormal];
        
    }
}
#pragma mark-修改了返点
-(void)setUserSelectedRebate:(NSNotification *)noti{
    
    NSDictionary *dic = noti.userInfo;
    MCShowBetModel * model = dic[@"MCShowBetModel"];
    self.baseWFmodel.userSelectedRebate=model.BetRebate;
    self.baseWFmodel.showRebate=model.showRebate;
    
    [self relayOutAlertViewData];
}

- (void)loadIssueData{
    

    if ([self.baseWFmodel.LotteryID isEqualToString:@"50"]) return;//秒秒彩不调用倒计时
    [self.view addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
    
    MCIssueModel *issueModel = [[MCIssueModel alloc] init];
    issueModel.lotteryNumber = [self.lotteriesTypeModel.LotteryID intValue];
    [issueModel refreashDataAndShow];
    self.issueModel = issueModel;
    __weak typeof(self) weakSelf = self;
    issueModel.callBackSuccessBlock = ^(id manager) {

        MCIssueModel *model = [MCIssueModel mj_objectWithKeyValues:manager];
        weakSelf.time= model.RemainTime;
        weakSelf.IssueNumber=model.IssueNumber;
        weakSelf.lastString = [NSString stringWithFormat:@"距%@期截止:",model.IssueNumber];
        weakSelf.baseWFmodel.IssueNumber=model.IssueNumber;
        [weakSelf setUpTimer];
        
    };
    
}

- (void)dealloc{
    NSLog(@"MCBaseLotteryHallViewController---------dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - SetUpUI
- (void)addNavRightBtn {
   
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, MC_REALVALUE(20), MC_REALVALUE(20));
    [rightBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"添加"] forState:UIControlStateNormal];
    UIBarButtonItem *rewardItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -7;
    self.navigationItem.rightBarButtonItems = @[spaceItem,rewardItem];
    
    
    /*
     * 倒计时栏
     */
    UIButton *btnDaojishi = [[UIButton alloc] init];
    [self.view addSubview:btnDaojishi];
    btnDaojishi.backgroundColor=RGB(245, 245, 245);
    btnDaojishi.frame = CGRectMake(0, 0, G_SCREENWIDTH, HEIGHTDAOJISHI);
    [btnDaojishi addTarget:self action:@selector(btnHistroyClick:) forControlEvents:UIControlEventTouchUpInside];
    self.btnDaojishi = btnDaojishi;
    
    UILabel *timerLabel = [[UILabel alloc] init];
    timerLabel.textColor=RGB(100, 100, 100);
    self.timerLabel = timerLabel;
    [btnDaojishi addSubview:timerLabel];
    timerLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    
    
    UILabel *timerDetailLabel = [[UILabel alloc] init];
    timerDetailLabel.layer.cornerRadius=3.0;
    timerDetailLabel.clipsToBounds=YES;
    timerDetailLabel.textColor=RGB(0, 0, 0);
    self.timerDetailLabel = timerDetailLabel;
    [btnDaojishi addSubview:timerDetailLabel];
    timerDetailLabel.font = [UIFont boldSystemFontOfSize:MC_REALVALUE(14)];
    
    
    UILabel *bonusLabel = [[UILabel alloc] init];
    bonusLabel.text = @"余额： ";
    [btnDaojishi addSubview:bonusLabel];
    bonusLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    
    
    UILabel *bonusDetailLabel = [[UILabel alloc] init];
    MCUserMoneyModel * userMoneyModel=[MCUserMoneyModel sharedMCUserMoneyModel];
    bonusDetailLabel.text = [NSString stringWithFormat:@"%@元",[MCMathUnits GetMoneyShowNum:userMoneyModel.LotteryMoney]];
    bonusDetailLabel.textColor=[UIColor redColor];
    [btnDaojishi addSubview:bonusDetailLabel];
    bonusDetailLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    _bonusDetailLabel=bonusDetailLabel;
    
    CGFloat padding = MC_REALVALUE(10);
    
    /*
     * 距离024期截止:
     */
    [timerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btnDaojishi.mas_left).offset(padding);
        make.centerY.equalTo(btnDaojishi.mas_centerY);
    }];
    [timerDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timerLabel.mas_right).offset(padding);
        make.centerY.equalTo(btnDaojishi.mas_centerY);
    }];
    
    [bonusDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btnDaojishi.mas_right).offset(-padding);
        make.centerY.equalTo(btnDaojishi.mas_centerY);
    }];
    
    [bonusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bonusDetailLabel.mas_left).offset(padding);
        make.centerY.equalTo(btnDaojishi.mas_centerY);
    }];
    

    
    float LeftScaleFactor=494/750.0;
    
    /*
     *  玩法名称
     */
    UIButton *btnPCatergy = [[UIButton alloc] init];
    [self.view addSubview:btnPCatergy];
    btnPCatergy.frame = CGRectMake(0, HEIGHTDAOJISHI, G_SCREENWIDTH*LeftScaleFactor, HEIGHTKAIJIANGHAO);
    btnPCatergy.backgroundColor = RGB(255, 255, 255);
    [btnPCatergy setImage:[UIImage imageNamed:@"btnPCatergy"] forState:UIControlStateNormal];
    [btnPCatergy setTitleColor:RGB(59, 126, 206) forState:UIControlStateNormal];
    CGFloat L=[NSString getWidthWithTitle:@"下拉按钮" font:MC_REALVALUE(14)];
    btnPCatergy.imageEdgeInsets = UIEdgeInsetsMake(0,L, 0, 0);
    btnPCatergy.titleEdgeInsets = UIEdgeInsetsMake(0,-25, 0, 0);
    [btnPCatergy setTitle:@"下拉按钮" forState:UIControlStateNormal];
    btnPCatergy.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    btnPCatergy.titleLabel.textAlignment = NSTextAlignmentCenter;
    btnPCatergy.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [btnPCatergy addTarget:self action:@selector(btnPCatergyClick:) forControlEvents:UIControlEventTouchUpInside];
    self.btnPCatergy = btnPCatergy;
    
    
    UIView * view_line=[[UIView alloc]init];
    view_line.backgroundColor=RGB(239, 239, 239);
    [btnPCatergy addSubview:view_line];
    view_line.frame=CGRectMake(G_SCREENWIDTH*LeftScaleFactor-1, 0, 1, HEIGHTKAIJIANGHAO);
    
    /*
     * 近期开奖
     */
    UIButton *btnKaiJiang = [[UIButton alloc] init];
    [self.view addSubview:btnKaiJiang];
    btnKaiJiang.frame = CGRectMake(G_SCREENWIDTH*LeftScaleFactor, HEIGHTDAOJISHI, G_SCREENWIDTH*(1-LeftScaleFactor), HEIGHTKAIJIANGHAO);
    btnKaiJiang.backgroundColor = RGB(255, 255, 255);
    [btnKaiJiang setImage:[UIImage imageNamed:@"btnKaiJiang"] forState:UIControlStateNormal];
    [btnKaiJiang setTitleColor:RGB(59, 126, 206) forState:UIControlStateNormal];
    [btnKaiJiang setTitle:@"近期开奖" forState:UIControlStateNormal];
    btnKaiJiang.titleLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(14)];
    CGFloat L2=[NSString getWidthWithTitle:@"近期开奖" font:MC_REALVALUE(14)]/2.0+G_SCREENWIDTH*(1-LeftScaleFactor)/2.0;
    
    btnKaiJiang.imageEdgeInsets = UIEdgeInsetsMake(0,L2, 0, 0);
    btnKaiJiang.titleEdgeInsets = UIEdgeInsetsMake(0,-25, 0, 0);
    btnKaiJiang.titleLabel.textAlignment = NSTextAlignmentCenter;
    btnKaiJiang.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    [btnKaiJiang addTarget:self action:@selector(btnKaiJiangClick:) forControlEvents:UIControlEventTouchUpInside];
    self.btnKaiJiang = btnKaiJiang;
    
}

- (void)setUpBottomView{
    
    MCAlertView *alertView = [MCAlertView alertInstance];
    [self.view addSubview:alertView];
    self.alertView = alertView;
    
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    self.bottomView.frame = CGRectMake(0, G_SCREENHEIGHT- MC_REALVALUE(49) -64, G_SCREENWIDTH, MC_REALVALUE(49));
    
    /*
     * 机选
     */
    MCPickBottomButton *randomBtn = [[MCPickBottomButton alloc] init];
    [bottomView addSubview:randomBtn];
    [randomBtn setTitle:@"机选" forState:UIControlStateNormal];
    [randomBtn setImage:[UIImage imageNamed:@"随机"] forState:UIControlStateNormal];
    [randomBtn setImage:[UIImage imageNamed:@"random_enabel"] forState:UIControlStateDisabled];
    self.randomBtn = randomBtn;
    randomBtn.backgroundColor=RGB(255, 255, 255);
    [randomBtn setTitleColor:RGB(83, 142, 202) forState:UIControlStateNormal];
    [randomBtn setTitleColor:RGB(140, 140, 140) forState:UIControlStateDisabled];
    
    randomBtn.tag = 1999;
    [randomBtn addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    /*
     * 清空
     */
    MCPickBottomButton *delAllBtn = [[MCPickBottomButton alloc] init];
    [bottomView addSubview:delAllBtn];
    delAllBtn.tag = 1998;
    delAllBtn.backgroundColor=RGB(255, 255, 255);
    [delAllBtn setTitleColor:RGB(83, 142, 202) forState:UIControlStateNormal];
    [delAllBtn setTitle:@"清空" forState:UIControlStateNormal];
    [delAllBtn setImage:[UIImage imageNamed:@"清空"] forState:UIControlStateNormal];
    [delAllBtn addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    /*
     * 分隔线
     */
    UIView * view_line=[[UIView alloc]init];
    [bottomView addSubview:view_line];
    view_line.backgroundColor=RGB(239, 239, 239);
    
    /*
     * 添加号码
     */
    MCPickBottomButton *addBtn = [[MCPickBottomButton alloc] init];
    addBtn.tag = 1997;
    self.addToShoppingCarBtn = addBtn;
    [bottomView addSubview:addBtn];
    addBtn.backgroundColor=RGB(77, 140, 210);
    [addBtn setTitle:@"添加号码" forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"添加"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *addItemCountLabel = [[UILabel alloc] init];
    addItemCountLabel.backgroundColor = [UIColor redColor];
    addItemCountLabel.layer.cornerRadius = MC_REALVALUE(10);
    addItemCountLabel.layer.masksToBounds = YES;
    addItemCountLabel.font = [UIFont systemFontOfSize:MC_REALVALUE(11)];
    addItemCountLabel.text = @"99+";
    addItemCountLabel.textAlignment = NSTextAlignmentCenter;
    addItemCountLabel.hidden = YES;
    addItemCountLabel.textColor = [UIColor whiteColor];
    
    self.addItemLabel = addItemCountLabel;
    
    /*
     * 去投注
     */
    MCPickBottomButton *gobyBtn = [[MCPickBottomButton alloc] init];
    gobyBtn.tag = 1996;
    gobyBtn.backgroundColor=RGB(17, 129, 214);
    
    [bottomView addSubview:gobyBtn];
    [gobyBtn setTitle:@"去投注" forState:UIControlStateNormal];
    [gobyBtn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [gobyBtn addTarget:self action:@selector(bottomViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [gobyBtn addSubview:addItemCountLabel];
    
    
    
    /*
     * 机选
     */
    [randomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView.mas_left);
        make.top.equalTo(bottomView.mas_top);
        make.bottom.equalTo(bottomView.mas_bottom);
//        make.width.equalTo(@(G_SCREENWIDTH/4.0));
        make.width.equalTo(@(G_SCREENWIDTH/6.0));
        
    }];
    
    /*
     * 分隔线
     */
    [view_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(randomBtn.mas_right);
        make.top.equalTo(bottomView.mas_top);
        make.bottom.equalTo(bottomView.mas_bottom);
        make.width.equalTo(@(0.5));
    }];

    
    /*
     * 清空
     */
    [delAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(randomBtn.mas_right);
        make.top.equalTo(bottomView.mas_top);
        make.bottom.equalTo(bottomView.mas_bottom);
        make.width.equalTo(@(G_SCREENWIDTH/6.0));
    }];
    
    
    
    /*
     * 添加号码
     */
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(delAllBtn.mas_right);
        make.top.equalTo(bottomView.mas_top);
        make.bottom.equalTo(bottomView.mas_bottom);
        make.width.equalTo(@(G_SCREENWIDTH/3.0));
    }];
    
    /*
     * 去投注
     */
    [gobyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addBtn.mas_right);
        make.top.equalTo(bottomView.mas_top);
        make.bottom.equalTo(bottomView.mas_bottom);
        make.width.equalTo(@(G_SCREENWIDTH/3.0));
    }];
    [addItemCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(gobyBtn.mas_right).offset(-MC_REALVALUE(5));
        make.top.equalTo(gobyBtn.mas_top).offset(MC_REALVALUE(5));
        make.height.equalTo(@(MC_REALVALUE(20)));
        make.width.equalTo(@(MC_REALVALUE(20)));
    }];
    
    

    /*
     * 投注页右上角功能按钮
     */
    MCMoreNavView * moreNavView=[[MCMoreNavView alloc]init];
    [self.view addSubview:moreNavView];
    moreNavView.frame=CGRectMake(G_SCREENWIDTH-WIDTH_MCMoreNav, 0, WIDTH_MCMoreNav, 0);
    _moreNavView=moreNavView;
    [_moreNavView.btn_PlayRecord addTarget:self action:@selector(action_PlayRecord) forControlEvents:UIControlEventTouchUpInside];
    [_moreNavView.btn_WFexplain addTarget:self action:@selector(action_WFexplain) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - registerNotifacation
- (void)registerNotifacation{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBorardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mcStackeUnitsGetBallWF:) name:@"MCSTAKEUNITS_GETBALL_WF" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideStackWindowss) name:@"MCSTAKEUNITS_HIDEN" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ballBtnClick) name:@"BALL_BTN_CLICK" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_YuanJiaoFen:) name:MCActionYuanJiaoFen object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_Multiple:) name:MCActionMultiple object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUserSelectedRebate:) name:McSelectedBetRebate object:nil];
    
}
- (void)hideStackWindowss{
    if (self.alertView.hidden == YES) {
        return;
    } else {
        [self.alertView hideStackWindow];
    }
    
}
#pragma mark-点击小球调用
- (void)ballBtnClick{
    self.randomBtnSelected = NO;
    //延迟调用的原因：self.baseWFmodel里面小球的选中信息没有及时更新过来，导致显示注数不对
    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.05];
}

-(void)delayMethod{
    MCBallPropertyModel *model =  [MCStakeUntits GetBallPropertyWithWFModel:self.baseWFmodel];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCSTAKEUNITS_GETBALL_WF" object:nil userInfo:@{@"ballModel":model}];
}
- (void)keyBorardWillShow:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;
    CGRect rect = [dic[@"UIKeyboardBoundsUserInfoKey"] CGRectValue];
    self.alertView.frame = CGRectMake(0, G_SCREENHEIGHT- rect.size.height - MC_REALVALUE(49) - MC_REALVALUE(97)-64, G_SCREENWIDTH, MC_REALVALUE(97));
    self.bottomView.frame = CGRectMake(0, G_SCREENHEIGHT- rect.size.height - MC_REALVALUE(49)-64, G_SCREENWIDTH, MC_REALVALUE(49));
    [self.alertView popViewHidden];
}

- (void)keyBoardWillHide:(NSNotification *)noti{
    
    self.alertView.frame = CGRectMake(0, G_SCREENHEIGHT - MC_REALVALUE(49) - MC_REALVALUE(97)-64, G_SCREENWIDTH, MC_REALVALUE(97));
    self.bottomView.frame = CGRectMake(0, G_SCREENHEIGHT - MC_REALVALUE(49)-64, G_SCREENWIDTH, MC_REALVALUE(49));
}
#pragma mark-修改了注数
- (void)mcStackeUnitsGetBallWF:(NSNotification *)noti{
    //    NSLog(@"%p",self);
    NSDictionary *dic = noti.userInfo;
    MCBallPropertyModel * model = dic[@"ballModel"];
    
    MCBallPropertyModel * model1 = dic[@"danshiModel"];
    if (model1 == nil) {
        if (model.stakeNumber >=1) {
            [self.alertView showStackWindow];
        }else{
            [self.alertView hideStackWindow];
            
        }
        
        self.baseWFmodel.stakeNumber=model.stakeNumber;
        [self relayOutAlertViewData];
        
    } else {
        if (model1.stakeNumber>=1) {
            [self.alertView showStackWindow];
        }else{
            [self.alertView hideStackWindow];
            
        }
        
        
        self.baseWFmodel.stakeNumber=model1.stakeNumber;
        [self relayOutAlertViewData];
    }
    
    
    
}
#pragma mark-修改了元角分
- (void)action_YuanJiaoFen:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;
    NSString * type= dic[@"type"];
    
    if ([type isEqualToString:@"元模式"]) {
        self.baseWFmodel.yuanJiaoFen=1;
    }else if ([type isEqualToString:@"角模式"]){
        self.baseWFmodel.yuanJiaoFen=0.1;
    }else if ([type isEqualToString:@"分模式"]){
        self.baseWFmodel.yuanJiaoFen=0.01;
    }else if ([type isEqualToString:@"厘模式"]){
        self.baseWFmodel.yuanJiaoFen=0.001;
    }
    [self relayOutAlertViewData];
    
}

#pragma mark-修改了倍数
- (void)action_Multiple:(NSNotification *)noti{
    NSDictionary *dic = noti.userInfo;
    NSString * multiple= dic[@"multiple"];
    self.baseWFmodel.multiple=[multiple intValue];
    [self relayOutAlertViewData];
}

#pragma mark-刷新弹框数据
-(void)relayOutAlertViewData{
    self.baseWFmodel.payMoney=self.baseWFmodel.stakeNumber*2*self.baseWFmodel.yuanJiaoFen*self.baseWFmodel.multiple;
    self.alertView.titleSelectedInfoLabel.text = [NSString stringWithFormat:@"已选%d注,共%@元",self.baseWFmodel.stakeNumber,GetRealFloatNum(self.baseWFmodel.payMoney)];
    
    
    if (self.baseWFmodel.maxAwardAmount&&self.baseWFmodel.minAwardAmount) {
        
        MCMoneyModel * MaxModel= MCGetBouns(self.baseWFmodel.maxAwardAmount, self.baseWFmodel.userSelectedRebate, self.baseWFmodel.yuanJiaoFen, self.baseWFmodel.multiple, self.baseWFmodel.payMoney);

        MCMoneyModel * MinModel= MCGetBouns(self.baseWFmodel.minAwardAmount, self.baseWFmodel.userSelectedRebate, self.baseWFmodel.yuanJiaoFen, self.baseWFmodel.multiple, self.baseWFmodel.payMoney);
 
        self.alertView.titleWinInfoLabel.text = [NSString stringWithFormat:@"若中奖，奖金%@~%@元,可盈利%@~%@元",MinModel.bouns,MaxModel.bouns,MinModel.yinli,MaxModel.yinli];
        
        
        
    }else if (self.baseWFmodel.maxAwardAmount){
        
        MCMoneyModel * MaxModel= MCGetBouns(self.baseWFmodel.maxAwardAmount, self.baseWFmodel.userSelectedRebate, self.baseWFmodel.yuanJiaoFen, self.baseWFmodel.multiple, self.baseWFmodel.payMoney);
        self.baseWFmodel.bonus=[MaxModel.bouns floatValue];

        self.baseWFmodel.yinli=[MaxModel.yinli floatValue];
        

        
        
        self.alertView.titleWinInfoLabel.text = [NSString stringWithFormat:@"若中奖，奖金%@元,可盈利%@元",MaxModel.bouns,MaxModel.yinli];
        
        self.baseWFmodel.profitChaseAwardAmount=GetRealFloatNum((self.baseWFmodel.bonus/self.baseWFmodel.multiple));

    }else{//无数据
        self.alertView.titleWinInfoLabel.text = [NSString stringWithFormat:@"若中奖，奖金--元,可盈利--元"];
    }
    
}

#pragma mark - timer
- (void)setUpTimer{
    [self stopTimer];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setTImerLabelInfo) userInfo:nil repeats:YES];
    self.timer = timer;
    [[NSRunLoop mainRunLoop ] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)setTImerLabelInfo{
    [self.activityIndicator stopAnimating];
    if (self.time == 0) {
        [self loadIssueData];
        MCHistoryIssueDetailAPIModel *historyIssueDetailAPIModel=[MCHistoryIssueDetailAPIModel sharedMCHistoryIssueDetailAPIModel];
        historyIssueDetailAPIModel.isNeedUpdate=NO;
        
    }
    if (self.IssueNumber.length>8&&[MCMathUnits isNeedCut:self.baseWFmodel.LotteryID]) {
        self.timerLabel.text = [NSString stringWithFormat:@"距%@期截止:",[self.IssueNumber substringFromIndex:8]];
    }else{
        self.timerLabel.text = [NSString stringWithFormat:@"距%@期截止:",self.IssueNumber];
    }
    
    self.time --;
    NSString *hour = [NSString stringWithFormat:@"%.2d",self.time /3600];
    NSString *min = [NSString stringWithFormat:@"%.2d",(self.time - [hour intValue]*3600)/60];
    NSString *sec = [NSString stringWithFormat:@"%.2d",self.time%60];
    self.timerDetailLabel.text = [NSString stringWithFormat:@"%@:%@:%@ ",hour,min,sec];
}

- (void)stopTimer{
    
    [self.timer invalidate];
    
    self.timer = nil;
}

#pragma mark-投注页右上角功能按钮
- (void)moreBtnClick:(UIButton *)btn{
    
    if (_isShowMoreBtn) {
        _isShowMoreBtn=NO;
    }else{
        _isShowMoreBtn=YES;
    }

    
    [UIView animateWithDuration:0.2 animations:^{
        
        if (_isShowMoreBtn) {
             _moreNavView.frame=CGRectMake(G_SCREENWIDTH-WIDTH_MCMoreNav, 0, WIDTH_MCMoreNav, HEIGHT_MCMoreNav);
        }else{
             _moreNavView.frame=CGRectMake(G_SCREENWIDTH-WIDTH_MCMoreNav, 0, WIDTH_MCMoreNav, 0);
        }
    } completion:^(BOOL finished) {
        
    }];
    
}
#pragma mark-跳转游戏记录
-(void)action_PlayRecord{
    [UIView animateWithDuration:0.2 animations:^{
        
        _moreNavView.frame=CGRectMake(G_SCREENWIDTH-WIDTH_MCMoreNav, 0, WIDTH_MCMoreNav, 0);
        
    } completion:^(BOOL finished) {
        _isShowMoreBtn=NO;
        MCGameRecordViewController * vc = [[MCGameRecordViewController alloc]init];

        [self.navigationController pushViewController:vc animated:YES];
    }];
    
}

#pragma mark-跳转玩法说明
-(void)action_WFexplain{
    [UIView animateWithDuration:0.2 animations:^{
        
        _moreNavView.frame=CGRectMake(G_SCREENWIDTH-WIDTH_MCMoreNav, 0, WIDTH_MCMoreNav, 0);
        
    } completion:^(BOOL finished) {
        _isShowMoreBtn=NO;
        MCWFExplainViewController * vc = [[MCWFExplainViewController alloc]init];
        vc.baseWFmodel=self.baseWFmodel;
        [self.navigationController pushViewController:vc animated:YES];
    }];
   
}


/** 玩法*/
- (void)btnPCatergyClick:(UIButton *)btn{

    MCPullMenuViewController *VC = [[MCPullMenuViewController alloc]init];
    VC.delegate=self;
    /*
     * 传入彩种ID
     */
    VC.lotteriesTypeModel = self.lotteriesTypeModel;//山东11选5
    MCPullMenuPresentationController *PC = [[MCPullMenuPresentationController alloc] initWithPresentedViewController:VC presentingViewController:self];
    PC.type=MCPullMenuCZListType;
    VC.transitioningDelegate = PC;
    [self presentViewController:VC animated:YES completion:NULL];
}
/** 近期开奖*/
-(void)btnKaiJiangClick:(UIButton *)btn{
    [self KaiJiangClick];
}
-(void)KaiJiangClick{
    
}
-(void)selectLotteryKindWithTag:(MCBasePWFModel*)baseWFmodel{

    

    self.baseWFmodel = baseWFmodel;
    
    
    /**圆角分模式*/
    self.baseWFmodel.yuanJiaoFen=1;
    /**奖金*/
    self.baseWFmodel.bonus=0;
    /**倍数*/
    self.baseWFmodel.multiple=1;
    /**金额*/
    self.baseWFmodel.payMoney=0;
    /**注数*/
    self.baseWFmodel.stakeNumber=0;
    /**盈利*/
    self.baseWFmodel.yinli=0;
    [self.alertView.msBtn setTitle:@"元模式" forState:UIControlStateNormal];
    self.alertView.btTF.text=@"1";
    [self.alertView.moneyBtn setTitle:@"1982.0.0" forState:UIControlStateNormal];
    /**彩种ID*/
    self.baseWFmodel.LotteryID=self.lotteriesTypeModel.LotteryID;
    self.baseWFmodel.czRebate=self.lotteriesTypeModel.MaxRebate;
    self.baseWFmodel.czTZRebate=self.lotteriesTypeModel.BetRebate;
    self.baseWFmodel.SaleState=self.lotteriesTypeModel.SaleState;
    /**彩种名称*/
    self.baseWFmodel.czName=self.lotteriesTypeModel.name;
    self.lotteriesTypeModel.playOnlyNum=self.baseWFmodel.playOnlyNum;
    self.lotteriesTypeModel.typeId=self.baseWFmodel.typeId;
    
    [self setWFPlayAward];
    
    [self setBetListMarry];
    
    [self.alertView hideStackWindow];
    
    
    if ([baseWFmodel.isMachineSelectEnabled isEqualToString:@"0"]) {
        self.randomBtn.enabled = NO;
    } else {
        self.randomBtn.enabled = YES;
    }
    
    NSString *str = [NSString stringWithFormat:@"%@_%@",baseWFmodel.typeName,baseWFmodel.name];
    [self.btnPCatergy setTitle:str forState:UIControlStateNormal];
    CGFloat L=[NSString getWidthWithTitle:str font:MC_REALVALUE(14)]/2.0+G_SCREENWIDTH*3/10.0+8;
    self.btnPCatergy.imageEdgeInsets = UIEdgeInsetsMake(0,L, 0, 0);
    
    if (baseWFmodel == nil) {
        return;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_LOTTERY_WF_ERJIWF" object:nil userInfo:@{@"baseModel":baseWFmodel}];
    
    if (_baseTableView) {
        [_baseTableView reloadData];
    }
    //延迟0.1秒是为了让Cell初始化完成
    [self performSelector:@selector(MC_PICKNUMBERVC_INIT) withObject:nil afterDelay:0.1];
    


}

- (void)loadData{
    
    self.baseWFmodel.maxAwardAmount=nil;
    self.baseWFmodel.minAwardAmount=nil;
    
        MCMaxbonusModel *bonusModel = [[MCMaxbonusModel alloc] init];
        bonusModel.lotteryNumber = [self.lotteriesTypeModel.LotteryID intValue];
        self.bonusModel = bonusModel;
        [bonusModel refreashDataAndShow];
    
    
        MCGetplayawardModel *playawardModel = [[MCGetplayawardModel alloc] init];
        playawardModel.lotteryNumber = [self.lotteriesTypeModel.LotteryID intValue];
        [playawardModel refreashDataAndShow];
        self.playawardModel = playawardModel;
        __block NSMutableArray *boModelArray = nil;
        __weak __typeof(self)wself = self;
//        dispatch_group_t group = dispatch_group_create();
//        dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//        dispatch_group_async(group, quene, ^{
    
        bonusModel.callBackFailedBlock = ^(id manager, NSString *errorCode) {
//              NSLog(@"complete task 1");
            
//               dispatch_semaphore_signal(semaphore);
        };
        bonusModel.callBackSuccessBlock = ^(id manager) {
            [wself.boModelArray removeAllObjects];
            boModelArray = [MCMaxbonusModel mj_objectArrayWithKeyValuesArray:manager];
            
          
            wself.boModelArray = boModelArray;
//              NSLog(@"complete task 1");
//              dispatch_semaphore_signal(semaphore);
        };
    
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//      });
    
//        dispatch_group_async(group, quene, ^{
    
            playawardModel.callBackSuccessBlock = ^(id manager){
    
                wself.playModelArray = [MCGetplayawardModel mj_objectArrayWithKeyValuesArray:manager];

//                dispatch_semaphore_signal(semaphore);
    
                [wself setWFPlayAward];
                

            };
            playawardModel.callBackFailedBlock = ^( id manager, NSString *errorCode) {

//                
//                dispatch_semaphore_signal(semaphore);
            };
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//       });
    
    
//        dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            
//            NSLog(@"完成了网络请求，不管网络请求失败了还是成功了。");
//
//        });
 
    
    
    
    
}
#pragma mark-设置玩法奖金
-(void)setWFPlayAward{
    for (MCGetplayawardModel * model in self.playModelArray) {
        if ([model.PlayCode isEqualToString:[NSString stringWithFormat:@"%@%@",self.baseWFmodel.LotteryID,self.baseWFmodel.methodId]]) {
            
            NSDictionary *dic1= model.AwardLevelInfo[0];
            if (model.AwardLevelInfo.count>1) {
            
                self.baseWFmodel.maxAwardAmount=[self getMaxandMinWithArr:model.AwardLevelInfo][@"MAX"];
                self.baseWFmodel.minAwardAmount=[self getMaxandMinWithArr:model.AwardLevelInfo][@"MIN"];
            
            }else{
                self.baseWFmodel.maxAwardAmount=dic1[@"AwardAmount"];
                self.baseWFmodel.minAwardAmount=nil;
            }
        }
    }
    [self relayOutAlertViewData];
}

-(NSDictionary *)getMaxandMinWithArr:(NSArray *)arr{

    float max=[arr[0][@"AwardAmount"] floatValue];
    float min=[arr[0][@"AwardAmount"] floatValue];
    
    for (NSDictionary *temp in arr) {

        if (max<[temp[@"AwardAmount"] floatValue]) {
            max=[temp[@"AwardAmount"] floatValue];
        }
        if (min>[temp[@"AwardAmount"] floatValue]) {
            min=[temp[@"AwardAmount"] floatValue];
        }
    }
    
    NSDictionary * dic=@{
                         @"MAX":GetRealFloatNum(max),
                         @"MIN":GetRealFloatNum(min)
                         };
    
    return dic;
    
}

- (void)modifyJsonFile{
//// 拿到本地json文件中的一段内容
//   NSMutableDictionary *dic = [MCLotteryID getWFIDByLotteryCategoriesID:@"1243" groupName:@"0"];
//    //增加一个字段
//    [dic setValue:@"2222" forKey:@"111"];
//    //拿到该文件
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"LotteryJSON_WFHelper" ofType:@"json"];
//    
////    [dic writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>]
//    //
//   NSFileManager *manager = [NSFileManager defaultManager];
////    [manager conte]
}
/** 历史*/
- (void)btnHistroyClick:(MCPickBottomButton *)btn{

}

- (void)bottomViewBtnClick:(MCPickBottomButton *)btn{
    
    if (btn.tag == 1999) {//机选
        [self randomBtnClick];
    }else if (btn.tag == 1998) {//清空
        [self clearAllButtonClick];
        [self.alertView hideStackWindow];
    }else if (btn.tag == 1997) {//添加号码
        [self addNumberToShoppingCar:YES];
//        [self.alertView hideStackWindow];
    }else if (btn.tag == 1996) {//去投注
        [self payTheSelectedNumbers];
        
        [self.alertView hideStackWindow];
    }
}

/**需要被覆盖的方法*/

#pragma mark - 需要被覆盖的方法
- (void)setUpUI{
    
}
- (void)randomBtnClick{
    self.randomBtnSelected = YES;
    
}

- (void)clearAllButtonClick{
    
}

- (void)addNumberToShoppingCar:(BOOL)isShow{
    
}

- (void)payTheSelectedNumbers{
    
}

//Push到菜种选择列表  将下拉弹回 ->在子类重写
-(void)pullLotteryRecordViewToTop{
    
}
-(void)MC_PICKNUMBERVC_INIT{
    
}

#pragma mark - getter and setter

- (NSMutableArray *)selectedCardArray{
    if (_selectedCardArray == nil) {
        _selectedCardArray = [NSMutableArray array];
    }
    return _selectedCardArray;
}
@end
